/**
 * Created by KhoaNam on 7/6/2014.
 */
var WaitLayer = MyLayer.extend({
   ctor: function(){
       this._super();
       this.slots = {};
       this.messBox = null;
       this.players = {};
   },

   init: function(){
       this._super();
       var size = cc.Director.getInstance().getWinSize();

       var sprite = cc.Sprite.create(INFO.GAME.BACKGROUND);
       sprite.setPosition(size.width/2, size.height/2);
       this.addChild(sprite);

       this.messBox = new MessageBox();
       this.messBox.addToLayer(this);

       //create 4 slot for user
       this.slots[1] = {
           x: size.width/2,
           y: size.height/10
       };
       this.slots[2] = {
           x: size.width - size.width/10,
           y: size.height/2
       };

       this.slots[3] = {
           x: size.width/2,
           y: size.height - size.height/10
       };

       this.slots[4] = {
           x: size.width/10,
           y: size.height/2
       };
   } ,

    /**
     * Add player to layer
     * @param players array of players
     */
    addPlayers: function(playersObj){
        var size = cc.Director.getInstance().getWinSize();
        this.messBox.setText(WAITING_MORE_PLAYERS);
        this.messBox.show(size.width/2, size.height/2);
        this.messBox.hideButton();
        //remove all players
        this.removeAllPlayers();
        var currPlayerObj = null;

        if(playersObj.length == 1){
            // this is current user
            this._addPlayer(playersObj[0], 1);
            currPlayerObj = playersObj[0];
        }
        else if(playersObj.length > 0 && playersObj.length <= 4){
            // find current player
            for(var i = 0 ; i < playersObj.length ; i++){
                if(playersObj[i].isCurrent == true){
                    currPlayerObj = playersObj[i];
                    break;
                }
            }

            for(var i = 0 ; i < playersObj.length ; i++){
                var delta = playersObj[i].position - currPlayerObj.position;
                if(delta < 0){
                    delta += 4;
                }
                this._addPlayer(playersObj[i], delta + 1);
            }
        }

        if(playersObj.length >= INFO.GAME.NUM_OF_PLAYER){
            conn.sfs.removeEventListener(SFS2X.SFSEvent.EXTENSION_RESPONSE, this._onResponseRequest, this);
            conn.sfs.addEventListener(SFS2X.SFSEvent.EXTENSION_RESPONSE, this._onResponseRequest, this);
            this.messBox.setText(WAITTING_OWNER_START);
            if(currPlayerObj.position != 1){
                this.messBox.showButton();
                this.messBox.getButtonSprite().onTap = function(touch, event){
                    console.log("ready button clicked");
                    conn.sfs.send( new SFS2X.Requests.System.ExtensionRequest("ready", null, conn.sfs.lastJoinedRoom));
                };
            }
            else{
                this.messBox.setText(WAITING_OTHERS_READY);
            }
        }
        players = this.players;
    },

    _onResponseRequest: function(event){
        var params = event.params;
        var cmd = event.cmd;

        console.log("extenstion response: " +  cmd);

        switch (cmd){
            case "ready":
                for(var i in this.players){
                    if(this.players[i] != undefined){
                        if(params.data.smartfox_user_id == this.players[i].id){
                            this.players[i].isReady = true;
                            if(this.players[i].isCurrent){
                                this.messBox.hideButton();
                            }
                        }
                    }
                }

                for(var i in this.players){
                    if(this.players[i] != undefined){
                        if(this.players[i].position == INFO.PLAYER.OWNER_POSITION && this.players[i].isCurrent){
                            if(this._isAllPlayerReady()){
                                this.messBox.showButton(INFO.MESSAGEBOX.BUTTON_START);
                                console.log("show start");
                                this.messBox.getButtonSprite().onTap = function(touch, event){
                                    console.log("clicked start button");
                                    conn.sfs.send( new SFS2X.Requests.System.ExtensionRequest("start", null, conn.sfs.lastJoinedRoom));
                                }
                            }
                        }
                    }
                }

                break;
            case "start":
                var dice = params.dice1 + params.dice2;
                var dora = params.dora;
                var currentPlayer = null;

                for(var i in this.players){
                    if(this.players[i].isCurrent == true){
                        currentPlayer = this.players[i];
                        break;
                    }
                }
                this.canCatchEvent = false;
                currentPlayer.leftTiles = params.lefttiles;
                currentPlayer.leftTilesOrderd = params.lefttilesInOrder;
                var director = cc.Director.getInstance();
                conn.sfs.removeEventListener(SFS2X.SFSEvent.EXTENSION_RESPONSE, this._onResponseRequest, this);
                director.replaceScene(new PlayScene(currentPlayer, dice, dora, params.dice1, params.dice2));
                break;
        }
    },

    _isAllPlayerReady: function(){
        for(var i in this.players){
            if(this.players[i].position != 1 && !this.players[i].isReady){
                return false;
            }
        }
        return true;
    },

    _addPlayer: function(Obj, index){
        this.players[index] = new Player();
        this.players[index].init(Obj);
        this.players[index].getAvatarSprite().setPosition(this.slots[index].x, this.slots[index].y);
        this.addChild(this.players[index].getAvatarSprite());

        var pos = this.players[index].getTextNamePosition();
        this.players[index].getNameSprite().setPosition(pos.x, pos.y);

        this.addChild(this.players[index].getNameSprite());

        pos = this.players[index].getDirectionIconPosition();
        this.players[index].getDirectionSprite().setPosition(pos.x, pos.y);

        this.addChild(this.players[index].getDirectionSprite());
    },

    _removePlayer: function(index){
        // remove in player
        if(this.players[index] != undefined){
            this.removeChild(this.players[index].getAvatarSprite());
            this.removeChild(this.players[index].getNameSprite());
            this.removeChild(this.players[index].getDirectionSprite());

            delete this.players[index];
            this.players[index] = undefined;
        }
    },

    removeAllPlayers: function(){
        for(var i = 1 ; i <= INFO.GAME.NUM_OF_PLAYER ; i++){
            this._removePlayer(i);
        }
    }
});